﻿using System;
using DapperExtensions.Mapper;
using Newtonsoft.Json;

namespace ZilLion.Service.TaskPlatform.Domain.Entity
{
    public class TaskStateDto
    {
        [JsonProperty("taskConfig")]
        public TaskConfig TaskConfig { get; set; }

        [JsonProperty("taskState")]
        public TaskStateEntity TaskState { get; set; }

        [JsonProperty("nodeInfo")]
        public NodeInfoEntity NodeInfo { get; set; }
    }

    [Serializable]
    public class TaskStateEntity
    {
        [JsonProperty("nodeId")]
        public string NodeId { get; set; }

        [JsonProperty("taskid")]
        public string Taskid { get; set; }
        [JsonProperty("taskname")]
        public string Taskname { get; set; }
        /// <summary>
        ///     作业执行状态 0-运行中，1-停止
        /// </summary>
        [JsonProperty("taskstatus")]
        public short Taskstatus { get; set; }

        /// <summary>
        ///     预计下次运行时间
        /// </summary>
        [JsonProperty("tasknextruntime")]
        public DateTime Tasknextruntime { get; set; } = DateTime.Now;

        /// <summary>
        ///     最后运行时间
        /// </summary>
        [JsonProperty("tasklastruntime")]
        public DateTime Tasklastruntime { get; set; } = DateTime.Now;

        /// <summary>
        ///     成功执行次数
        /// </summary>
        [JsonProperty("taskexcutedcount")]
        public int TaskExcutedCount { get; set; }

        /// <summary>
        ///     失败次数
        /// </summary>
        [JsonProperty("taskerrorcount")]
        public int TaskErrorCount { get; set; }
    }

    public sealed class TaskRunLogMapper : ClassMapper<TaskStateEntity>
    {
        public TaskRunLogMapper()
        {
            Table("task_state");
            Map(f => f.NodeId).Key(KeyType.Assigned).Column("nodeid");
            Map(f => f.Taskid).Key(KeyType.Assigned).Column("taskid");
            Map(f => f.Taskname).Column("taskname");
            Map(f => f.Taskstatus).Column("taskstatus");
            Map(f => f.Tasknextruntime).Column("tasknextruntime");
            Map(f => f.Tasklastruntime).Column("tasklastruntime");
            Map(f => f.TaskErrorCount).Column("taskerrorcount");
            Map(f => f.TaskExcutedCount).Column("taskexcutedcount");
        }
    }
}